package com.ruoyi.dispatch.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.dispatch.enums.OnDutyReportCol;
import com.ruoyi.dispatch.vo.OnDutyReportVO;
import com.ruoyi.framework.utils.BeanCopyUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dispatch.domain.OnDutyReport;
import com.ruoyi.dispatch.service.IOnDutyReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调度值班Controller
 * 
 * @author wuxiaofang
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/dispatch/onDutyReport")
@Anonymous
public class OnDutyReportController extends BaseController
{
    @Autowired
    private IOnDutyReportService onDutyReportService;

    /**
     * 查询调度值班列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:onDutyReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(OnDutyReport onDutyReport,String[] columnArray)
    {
        startPage();
        List<OnDutyReport> list = onDutyReportService.selectOnDutyReportList(onDutyReport);
        //组装展示列名信息
        List<Map<String,String>> columns = null;
        if(columnArray!=null && columnArray.length!=0){
            columns = onDutyReportService.handleColumnMap(columnArray);
        }
        TableDataInfo tableData =  getDataTable(list);
        tableData.setColumns(columns);

        return tableData;
    }

    /**
     * 导出调度值班列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:onDutyReport:export')")
    @Log(title = "调度值班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OnDutyReport onDutyReport)
    {
        List<OnDutyReport> list = onDutyReportService.selectOnDutyReportList(onDutyReport);
        ExcelUtil<OnDutyReport> util = new ExcelUtil<OnDutyReport>(OnDutyReport.class);
        util.exportExcel(response, list, "调度值班数据");
    }

    /**
     * 获取调度值班详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:onDutyReport:query')")
    @GetMapping(value = "/{dutyId}")
    public AjaxResult getInfo(@PathVariable("dutyId") Long dutyId)
    {
        return success(onDutyReportService.selectOnDutyReportByDutyId(dutyId));
    }

    /**
     * 新增调度值班
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:onDutyReport:add')")
    @Log(title = "调度值班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OnDutyReport onDutyReport)
    {
        //插入数据
        int insertResult = onDutyReportService.insertOnDutyReport(onDutyReport);
        //查询此次新增的记录
        OnDutyReport report = onDutyReportService.selectOnDutyReportByDutyId(onDutyReport.getDutyId());
        OnDutyReportVO vo = BeanCopyUtils.copyProperties(report,OnDutyReportVO.class);

        return toAjax(insertResult,vo);
    }

    /**
     * 修改调度值班
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:onDutyReport:edit')")
    @Log(title = "调度值班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OnDutyReport onDutyReport)
    {
        return toAjax(onDutyReportService.updateOnDutyReport(onDutyReport));
    }

    /**
     * 删除调度值班
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:onDutyReport:remove')")
    @Log(title = "调度值班", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dutyIds}")
    public AjaxResult remove(@PathVariable Long[] dutyIds)
    {
        return toAjax(onDutyReportService.deleteOnDutyReportByDutyIds(dutyIds));
    }

    @RequestMapping("genMonthDutyReport")
    public AjaxResult genMonthDutyReport(String month){
        List<OnDutyReport> list = onDutyReportService.copyRecentlyMonthReport(month);
        List<OnDutyReportVO> vo = BeanCopyUtils.copyListProperties(list,OnDutyReportVO.class);
        return toAjax(vo.size(),vo);
    }
}
